const AlipaySdk = require('alipay-sdk');
const express = require("express");
const fs = require('node:fs')
const alipaySdk = new AlipaySdk({
    appId: '2021004139609016',
    // keyType: 'PKCS1',
    // signType: 'RSA2',
    gateway: 'https://openapi.alipay.com/gateway.do',
    privateKey: fs.readFileSync('./config/appPrivate.pem', 'ascii'),
    alipayPublicKey: fs.readFileSync('./config/alipayPublic.pem', 'ascii'),
    postCharset: "UTF-8",
    format: "json"
});
var router = express.Router();

/* GET home page. */
router.get('/getToken', async function (req, res, next) {
    let result
    try {
        const r = await alipaySdk.exec('alipay.system.oauth.token', {
            grant_type: "authorization_code",//refresh_token
            code: req.query.code
        })
        console.log(r);
        result = {
            code: 200,
            data: r
        }
    } catch (error) {
        console.log(error);
        result = {
            code: 500,
            message: error,
        }
    }
    res.send(result)
});
/* GET home page. */
router.get('/getInfo', async function (req, res, next) {
    const result = await alipaySdk.exec('alipay.open.public.info.query');
    res.send(result)
});

module.exports = router;
